package com.nisco.dms.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nisco.dms.entity.Dictionary;
import com.nisco.dms.service.DictionaryService;
import com.nisco.dms.util.dwz.AjaxObject;
import com.nisco.dms.util.dwz.Page;
@Controller
@RequestMapping(value="/dict")
public class DictionaryController
{
	@Resource
	private DictionaryService dictionaryService;
	
	private static final String LIST="dictionary/dictionaryList";
	private static final String CREATE="dictionary/dictionaryCreate";
	private static final String UPDATE="dictionary/dictionaryUpdate";
	
	@RequestMapping(value="/list")
	public String list(Page page,Model model,String keywords)
	{
		if(keywords!=null&&!"".equals(keywords))
			model.addAttribute("dicts", dictionaryService.findDictBykeywords(page, keywords));
		else
			model.addAttribute("dicts", dictionaryService.findAll(page));
		model.addAttribute("page", page);
		return LIST;
	}
	@RequestMapping(value="/preCreate")
	public String preCreate()
	{
		return CREATE;
	}
	@RequestMapping(value="/preUpdate/{id}")
	public String preUpdate(@PathVariable(value="id") String id,Model model)
	{
		model.addAttribute("dict", dictionaryService.get(id));
		return UPDATE;
	}
	
	@RequestMapping(value="/create")
	public @ResponseBody String create(Dictionary dict)
	{
		dictionaryService.save(dict);
		AjaxObject ajaxObject=new AjaxObject();
		ajaxObject.setMessage("词典增加成功!");
		ajaxObject.setNavTabId("moduleListNav_2c92b8113e43d8f5013e443605b10001");
		return ajaxObject.toString();
	} 
	
	@RequestMapping(value="/update")
	public @ResponseBody String update(Dictionary dict)
	{
		dictionaryService.update(dict);
		AjaxObject ajaxObject=new AjaxObject();
		ajaxObject.setMessage("词典修改成功!");
		ajaxObject.setNavTabId("moduleListNav_2c92b8113e43d8f5013e443605b10001");
		return ajaxObject.toString();
	}
	@RequestMapping(value="/delete/{id}")
	public @ResponseBody String delete(@PathVariable(value="id") String id)
	{
		dictionaryService.delete(id);
		AjaxObject ajaxObject=new AjaxObject();
		ajaxObject.setMessage("词典删除成功!");
		ajaxObject.setCallbackType("");
		return ajaxObject.toString();
	}

}
